﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Criterion;

namespace PhotoBook.Repositorio
{
    public class RepositorioCategoria : RepositorioBase, PhotoBook.Model.NHibernate.IRepositorioCategoria
    {
        #region IGenericRepository<Categoria> Members

        public int Count()
        {
            throw new NotImplementedException();
        }

        public void Delete(PhotoBook.Model.Entities.Categoria obj)
        {
            using (ISession session = this.SessionNHiBernate.OpenSession())
            {
                using (session.BeginTransaction())
                {
                    session.Delete(obj);
                    session.Transaction.Commit();
                }                
            }
        }

        public int Insert(PhotoBook.Model.Entities.Categoria obj)
        {
            using (ISession session = this.SessionNHiBernate.OpenSession())
            {
                using (session.BeginTransaction())
                {
                    int id = (int)session.Save(obj);
                    session.Transaction.Commit();
                    return id;                    
                }
            }
        }

        public IList<PhotoBook.Model.Entities.Categoria> Select()
        {
            using (ISession session = this.SessionNHiBernate.OpenSession())
            {
                ICriteria criteria = session.CreateCriteria(typeof(PhotoBook.Model.Entities.Categoria));
                return criteria.List<PhotoBook.Model.Entities.Categoria>();
            }
        }

        public PhotoBook.Model.Entities.Categoria SelectById(object id)
        {
            using (ISession session = this.SessionNHiBernate.OpenSession())
            {
                return session.Get<PhotoBook.Model.Entities.Categoria>(id);
            }
        }

        public void Update(PhotoBook.Model.Entities.Categoria obj)
        {
            using (ISession session = this.SessionNHiBernate.OpenSession())
            {
                using (session.BeginTransaction())
                {
                    session.Update(obj);
                    session.Transaction.Commit();
                }                
            }
        }

        public IList<PhotoBook.Model.Entities.Categoria> BuscaCategoriaByExample(PhotoBook.Model.Entities.Categoria categoria)
        {
            using (ISession session = this.SessionNHiBernate.OpenSession())
            {                
                ICriteria query = session.CreateCriteria<PhotoBook.Model.Entities.Categoria>();
                query.Add(Example.Create(categoria));                      
                return query.List<PhotoBook.Model.Entities.Categoria>();
            }
        }


        #endregion
    }
}
